import { Viewer } from 'cesium'
import { defaultConfig } from '@/cesium'

const viewerMap: Map<string, Viewer> = new Map()

export const DEFAULT_NAME = defaultConfig.DEFAULT_NAME

export function createViewer(
  name: string = DEFAULT_NAME,
  opt: Viewer.ConstructorOptions = {}
) {
  const viewer = new Viewer(name, opt)
  viewerMap.set(name, viewer)
  return viewer
}

/**
 * 获取Viewer
 * @param name
 */
export function getViewer(name = DEFAULT_NAME) {
  return viewerMap.get(name)
}

export function deleteViewerFromMap(name = DEFAULT_NAME) {
  viewerMap.delete(name)
}

/**
 * 摧毁Viewer
 * @param name
 */
export function destroyViewer(name: string) {
  const _viewer = getViewer(name)
  _viewer?.destroy()
  deleteViewerFromMap()
}

/**
 * 清空Viewer视图
 * @param name
 * @constructor
 */
export function ClearViewer(name: string) {
  const _viewer = getViewer(name)
}

/**
 * 判断是不是viewer
 * @param data
 */
export function isViewer(data: string | Viewer): data is Viewer {
  return data instanceof Viewer
}
